package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.util.PageList;
import com.frogs.system.util.AttentionStateEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.UserAttentionInfoDao;
import com.frogs.ucenter.dao.UserAttentionRequestLogDao;
import com.frogs.ucenter.entity.UserAttentionRequestLog;
import com.frogs.ucenter.entity.ext.UserAttentionInfoExt;
import com.frogs.ucenter.v1.request.UserAttentionRequestLogAddRequest;
import com.frogs.ucenter.v1.request.UserAttentionRequestLogListRequest;
import com.frogs.ucenter.v1.request.UserAttentionRequestLogUpdateRequest;
import com.frogs.ucenter.v1.response.AddResponse;
import com.frogs.ucenter.v1.response.ShowResponse;
import com.frogs.ucenter.v1.response.UpdateResponse;
import com.frogs.ucenter.v1.service.UserAttentionRequestLogService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service(ApiConstants.VERSION+"userAttentionRequestLogService")
public class UserAttentionRequestLogServiceImpl implements UserAttentionRequestLogService {

	private static final Logger log = LoggerFactory.getLogger(UserBaseInfoServiceImpl.class);
	
	@Resource
    private UserAttentionRequestLogDao userAttentionRequestLogDao;
	@Resource
	private UserAttentionInfoDao userAttentionInfoDao;

	@Override
	public Response add(Request request) {
		AddResponse<UserAttentionRequestLog> response = new AddResponse<UserAttentionRequestLog>();
		try{
			UserAttentionRequestLogAddRequest uarlar = (UserAttentionRequestLogAddRequest)request;
			Integer sendUserId = uarlar.getSendUserId();
			UserAttentionRequestLog userAttentionRequestLog = new UserAttentionRequestLog();
			userAttentionRequestLog.setSendUserId(sendUserId);
			//请求中状态日志的操作者id为0
			userAttentionRequestLog.setOperatorUserId(0);
			//添加关注日志时添加请求中的状态
			userAttentionRequestLog.setStatus(AttentionStateEnum.ATTENTION_STATUS_REQUEST.getId());
			userAttentionRequestLog.setUpdateTime(new Date());
			userAttentionRequestLogDao.insert(userAttentionRequestLog);
			response.setInfo(userAttentionRequestLog);
			response.setTip("请求关注日志添加成功");
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

	@Override
	public Response update(Request request) {
		UpdateResponse<UserAttentionRequestLog> response = new UpdateResponse<UserAttentionRequestLog>();
		try{
			UserAttentionRequestLogUpdateRequest uarlur = (UserAttentionRequestLogUpdateRequest)request;
			Integer id = uarlur.getId();
			UserAttentionRequestLog userAttentionRequestLog = new UserAttentionRequestLog();
			userAttentionRequestLog = userAttentionRequestLogDao.selectById(id);
			if(userAttentionRequestLog.getStatus()!=AttentionStateEnum.ATTENTION_STATUS_REQUEST.getId()){
				throw new ServiceOperationException("该关注请求已经被编辑过了！");
			}
			Integer sendUserId = uarlur.getSendUserId();
			Integer operatorUserId = uarlur.getOperatorUserId();
			Integer status = uarlur.getStatus();
			userAttentionRequestLog.setId(id);
			userAttentionRequestLog.setSendUserId(sendUserId);
			userAttentionRequestLog.setOperatorUserId(operatorUserId);
			userAttentionRequestLog.setStatus(status);
			userAttentionRequestLog.setUpdateTime(new Date());
			userAttentionRequestLogDao.update(userAttentionRequestLog);
			response.setInfo(userAttentionRequestLog);
			response.setTip("请求关注日志更新成功");
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

	@Override
	public Response getDetail(Request request) {
		ShowResponse<Map<String,Object>> response = new ShowResponse<Map<String,Object>>();
		try{
			UserAttentionRequestLogListRequest uarllr = (UserAttentionRequestLogListRequest)request;
			Integer logId = uarllr.getLogId();
			Integer userId = uarllr.getUserId();
			Map<String, Object> params = new HashMap<String, Object>();
			Map<String, Object> listParams = new HashMap<String, Object>();
			params.put("eqId", logId);
			UserAttentionRequestLog userAttentionRequestLog = userAttentionRequestLogDao.selectByMap(params, 1, 1).get(0);
			Integer sendUserId = userAttentionRequestLog.getSendUserId();
			params.clear();
			listParams.put("verifyAttention", false);
			if(!sendUserId.equals(userId)){
				params.put("eqFollowUserId", sendUserId);
				params.put("eqFollowedUserId", userId);
				params.put("eqStatus", AttentionStateEnum.ATTENTION_STATUS_AGREED.getId());
				PageList<UserAttentionInfoExt> infoExts = userAttentionInfoDao.selectByMapForAttention(params, 1, 1);
				if (infoExts.size() > 0) {
					listParams.put("verifyAttention", true);
				}
			}else{
				if(userAttentionRequestLog.getStatus() == AttentionStateEnum.ATTENTION_STATUS_AGREED.getId()){
					listParams.put("verifyAttention", true);
				}
			}
			listParams.put("userAttentionRequestLog", userAttentionRequestLog);
			response.setInfo(listParams);
			response.setTip("查询关注日志成功");
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

}